package com.recorder;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.recorder.recorder_lib.PlayManager;

import java.io.File;
import java.util.List;

/**
 * desc   :
 * author : guowenlong
 * Email  : guowenlong20000@gmail.com
 * time   : 2018年12月21日17 : 46
 */
public class DirAdapter extends BaseAdapter {
    private List<File> list;

    public DirAdapter(List<File> dirPath) {
        list = dirPath;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_file, null);
        }
        TextView tvFilName = convertView.findViewById(R.id.tv_item_filename);
        TextView btnDelete = convertView.findViewById(R.id.btn_item_delete);
        TextView btnPlay = convertView.findViewById(R.id.btn_item_play);

        tvFilName.setText(list.get(position).getName());
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).delete()) list.remove(position);
                notifyDataSetChanged();
            }
        });
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayManager.getInstance().playSound(list.get(position).getAbsolutePath());
            }
        });
        return convertView;
    }
}